package com.dtf.admin.filter;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dtf.admin.staff.vo.SystemStaff;
import com.dtf.core.common.filter.WrapperResponse;
import com.dtf.core.common.utils.Const;
import com.dtf.core.common.utils.Contanst;

public class ContentChangeFilter implements Filter{
	
	private static String frame_0 = "0";	//默认框架,包括左侧菜单、系统抬头、系统底部
	private static String frame_1 = "1";	//空白框架，只有一个框架，无菜单等内容
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain fc) throws IOException, ServletException {
		String url = ((HttpServletRequest)request).getRequestURI();
		
		//先判断是否是公共可访问的页面
		String[] noLoginPage = new String[]{
				"/resource/",//公共页面404、503、error等页面
				"/admin/login."
		};
		for (String p : noLoginPage) {
			if (url.startsWith(p)) {
				fc.doFilter(request, response);
				return;
			}
		}
		
		HttpSession session =  ((HttpServletRequest)request).getSession();
		SystemStaff staff = (SystemStaff) session.getAttribute(Contanst.CURRENT_USER_KEY);
		//如果未登录，直接转到登录页面
		if(staff == null){
			((HttpServletResponse)response).sendRedirect("/admin/login.jsp");
			return;
		}
		String frame = request.getParameter("frame");
		
		//先校验权限，判断是否需要登录，判断是否没有权限访问
		try {
			if (Const.strIsEmpty(frame) && checkLogin(request, response,fc)) {
				return;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//计算需要自动返回哪一种外层框架
		frame = calculationFrameType(request);
		
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		if (frame_0.equals(frame)) {
			WrapperResponse cr = new WrapperResponse((HttpServletResponse)response);
			fc.doFilter(request, cr);
			
			//处理替换
			String content = cr.getContent();
			request.setAttribute("page-content", content);
			request.getRequestDispatcher("/admin/frame/frame_default.jsp").forward(request, response);
			
		}else if(frame_1.equals(frame)) {
			WrapperResponse cr = new WrapperResponse((HttpServletResponse)response);
			fc.doFilter(request, cr);
			
			//处理替换
			String content = cr.getContent();
			request.setAttribute("page-content", content);
			request.getRequestDispatcher("/admin/frame/frame_blank.jsp").forward(request, response);
		}else{
			fc.doFilter(request, response);
		}
	}
	
	/**
	 * 判断是否需要登录才能访问页面
	 * @param url
	 * @return
	 * @throws Exception 
	 */
	public boolean checkLogin(ServletRequest request, ServletResponse response,FilterChain fc) throws Exception{
		String url = ((HttpServletRequest)request).getRequestURI();
		
		HttpSession session =  ((HttpServletRequest)request).getSession();
		
		//如果没有权限，直接转到404页面
		List<Map<String, String>> menus = (List<Map<String, String>>) session.getAttribute(Contanst.CURRENT_MENU_KEY);
		for (Map<String, String> map : menus) {
			String menu_url = Const.getStrValue(map, "menu_url");
			if (menu_url.startsWith(url)) {
				String menu_path = Const.getStrValue(map, "menu_path");
				request.setAttribute(Contanst.CURRENT_MENU_NOW_PATH, menu_path);
				return false;	//表示不用登录也可访问
			}
		}
		
		//如果是登录了，没有权限的情况下，直接转到404页面
		((HttpServletResponse)response).sendRedirect("/resource/404.jsp");
		return true;
	}
	
	/**
	 * 计算需要自动返回哪一种外层框架
	 * null:不需要添加外层框架
	 * "":默认框架
	 * 1：
	 * @return
	 */
	public String calculationFrameType(ServletRequest request){
		String url = ((HttpServletRequest)request).getRequestURI();
		
		//null:不需要添加外层框架
		if (isNoFrame(url)) {
			return null;
		}
		
		String frame = request.getParameter("frame");
		
		if (Const.strIsEmpty(frame)) {
			frame = frame_0;
		}
		return frame;
	}
	
	private boolean isNoFrame(String url){
		String[] noFramePage = new String[]{
				"/login.",	//登录页面
				"/resource/",//公共页面404、503、error等页面
				"/admin/login."
		};
		for (String p : noFramePage) {
			if (url.startsWith(p)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
